<?php

require_once("DBSettings.php");

/**
 * Function to override internal errorHandler
 * @access Private
 * @param - 
 * @return True to make sure the internal handler does not execute
 */

//Global variable
$didErrorOccure = false;

//Global function
function myErrorHandler($errno, $errstr, $errfile, $errline){
    LogHandler::logError("");
    LogHandler::logError("Critical DataBase Error: ");
	LogHandler::logError("ErrorNumber: " . $errno);
	LogHandler::logError("ErrorString: " . $errstr);
	LogHandler::logError("File: " . $errfile);
	LogHandler::logError("Line: " . $errline);
    $didErrorOccure = true;
    
    /* Don't execute PHP internal error handler */
    return true;
}

class DBConnection{
	
	public $m_mysqli = null;
	
	public function __construct(){
		//Comment row before running the Unittests
		set_error_handler("myErrorHandler");
	}

	/**
	 * Function to create an mysqli object and connect
	 * @Access Public
	 * @Param -
	 * @Return Bool
	 */ 
	public function connect(){
		//Create our Msqli object
		$this->m_mysqli = new mysqli(DBSettings::DBHOST,
									 DBSettings::DBUSER,
									 DBSettings::DBPASSWORD,
									 DBSettings::DATABASE);
									 
	 	//Set charset to utf-8
	 	$this->m_mysqli->set_charset("UTF-8");
		
		//Check if we have a connect
		if($this->m_mysqli->connect_errno){
			LogHandler::logError("Connect failed: " . $this->m_mysqli->connect_error);
			NavigationView::setHeadeLocation("dberror.html");
			return false;
		}	
		else{
			return true;
		}
	}
	
	/**
	 * Function to close connection
	 * @Access Public
	 * @Param -
	 * @Return Bool
	 */
	public function close(){
		$this->m_mysqli->close();
	}
	
	/**
	 * Function to prepare a statement
	 * @Access Public
	 * @Param sql query 
	 * @Return prepared statment
	 */ 
	public function prepare($sql){
		$stmt = $this->m_mysqli->prepare($sql);
		if($stmt === false){
			LogHandler::logError("Prepare failed: ");
			NavigationView::setHeadeLocation("dberror.html");	
			return false;
		}
		else{
			return $stmt;
		}
	}
	
	/**
	 * Function to runquery
	 * @Access public 
	 * @Param SQL query
	 * @return Bool
	 */
	public function RunQuery($sql){
		$result = $this->m_mysqli->query($sql);
			
		if($result === FALSE){
			LogHandler::logError("RunQuery in DBConnection can not run query: " . $sql);
			NavigationView::setHeadeLocation("dberror.html");
			return false;
		}
		return true;
	}
	
	/**
	 * Function to drop table
	 * @Access Public
	 * @Param string Tablename
	 * @return bool
	 */
	public function dropTable($tabelName){
		$sql = "DROP TABLE $tabelName";
		
		$result = $this->m_mysqli->query($sql);
		
		if( $result === FALSE){
			LogHandler::logError("dropTable in DBConnection can not run query: " . $sql);
			NavigationView::setHeadeLocation("dberror.html");
			return false;
		}
		return true;
	}
	
	/**
	 * @Access public
	 * @Param mysqli stmt, string with classname who is calling, string with name of primary key for indexing the return array
	 * @Return Array with objects from DB
	 * TODO: Move functionality back to calling Handler, get_result is not installed on host
	 */
	public function fetchObjects(mysqli_stmt $stmt, $class, $nameOfPrimareKey){
 		$retArray = array();
		if($stmt->execute() === false){
			LogHandler::logError("fetchObjects in DBConnection can not execute! ");
			return $retArray;
        }
		
		$result = $stmt->get_result();
		
		//As long as we can fetch objects, store them in the array
		while($object = $result->fetch_object($class)){
			$retArray[$object->$nameOfPrimareKey] = $object;
		}
		
		//When done, close
		$stmt->close();
		return $retArray;		
	}
}


